import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach(async (to, from, next) => {
    // 进度条开始
    NProgress.start()

    // 设置页面title
    document.title = getPageTitle(to.meta.title)

    // 判断用户是否登录
    const hasToken = getToken()

    if (hasToken) {
        if (to.path === '/login') {
            // 如果已登录跳转登录页，则重定向到主页
            next({ path: '/' })
            NProgress.done()
        } else {
            // 是否有路由表
            if (store.getters.routes.length > 0) {
                next()
            } else {
                try {
                    // // 获取管理员信息
                    // store.dispatch('user/getInfo')
                    // // 获取菜单树
                    // const menuTree = await store.dispatch('user/getMenuTree')
                    const menuTree = [{
                        "id": 250,
                        "parentId": 0,
                        "title": "合作意向",
                        "level": 0,
                        "sort": 5,
                        "name": "",
                        "icon": "",
                        "hidden": 0,
                        "path": "/contactManage",
                        "redirect": "",
                        "component": "layout",
                        "type": 0,
                        "resources": null,
                        "children": [
                            {
                                "id": 251,
                                "parentId": 250,
                                "title": "合作意向",
                                "level": 1,
                                "sort": 0,
                                "name": "",
                                "icon": "",
                                "hidden": 0,
                                "path": "post_index",
                                "redirect": "",
                                "component": "contactManage/index",
                                "type": 0,
                                "resources": null,
                                "children": [
                                    {
                                        "id": 253,
                                        "parentId": 251,
                                        "title": "列表",
                                        "level": 2,
                                        "sort": 0,
                                        "name": "",
                                        "icon": "",
                                        "hidden": 0,
                                        "path": "",
                                        "redirect": "",
                                        "component": "",
                                        "type": 0,
                                        "resources": [
                                            "/upsPost/list"
                                        ],
                                        "children": []
                                    },
                                    {
                                        "id": 254,
                                        "parentId": 251,
                                        "title": "详情",
                                        "level": 2,
                                        "sort": 0,
                                        "name": "",
                                        "icon": "",
                                        "hidden": 0,
                                        "path": "",
                                        "redirect": "",
                                        "component": "",
                                        "type": 0,
                                        "resources": [
                                            "/upsPost/detail"
                                        ],
                                        "children": []
                                    }
                                ]
                            },
                            {
                                "id": 256,
                                "parentId": 250,
                                "title": "合作意向详情",
                                "level": 1,
                                "sort": 2,
                                "name": "",
                                "icon": "",
                                "hidden": 1,
                                "path": "post_detail",
                                "redirect": "",
                                "component": "contactManage/detail",
                                "type": 0,
                                "resources": null,
                                "children": [
                                    {
                                        "id": 257,
                                        "parentId": 256,
                                        "title": "审核帖子",
                                        "level": 2,
                                        "sort": 0,
                                        "name": "",
                                        "icon": "",
                                        "hidden": 0,
                                        "path": "",
                                        "redirect": "",
                                        "component": "",
                                        "type": 0,
                                        "resources": [
                                            "/upsPost/auditPost"
                                        ],
                                        "children": []
                                    }
                                ]
                            }
                        ]
                    }]
                    // 生成可访问路由映射
                    const accessRoutes = await store.dispatch('user/generateRoutes', menuTree)
                    // 动态添加可访问路由
                    router.addRoutes(accessRoutes)

                    // hack方法来确保adroutes是完整的
                    // 设置replace: true，这样导航就不会留下历史记录
                    next({ ...to, replace: true })
                } catch (error) {
                    // 删除令牌并转到登录页面重新登录
                    await store.dispatch('user/resetToken')
                    Message.error(error || 'Has Error')
                    next(`/login?redirect=${to.path}`)
                    NProgress.done()
                }
            }
        }
    } else {
        // 没有token
        if (whiteList.indexOf(to.path) !== -1) {
            // 在免登录白名单中，直接进入
            next()
        } else {
            // 其他没有访问权限的页面被重定向到登录页面。
            next(`/login?redirect=${to.path}`)
            NProgress.done()
        }
    }
})

router.afterEach(() => {
    // 进度条结束
    NProgress.done()
})
